<?php

class HmStore_HmStoreController extends Fw_Controller_Action
{
	public function init()
	{
		parent::init();
		$this->setTemplatePath('hm-store');
	}
	
	public function listAction()
	{
		$model      = $this->_getModel('HmStore');
		$store_list = $this->_model;
		$rowset     = $store_list->fetchAll();

		$rowcount = count($rowset);
		$this->view->store_list = $rowset;
		$this->view->rowcount   = $rowcount;
	}
	
	public function showAction()
	{
		$store_id = (int)$this->_request->getParam('store_id');		
		$model      = $this->_getModel('HmStore');
		$store_show = $this->_model;
		$this->view->store_show = $store_show->fetchRow('store_id='.$store_id);
	}
	
	public function addAction()
	{
		$this->initSession();
		if ($this->_request->isPost()) {
			$formData = $this->_request->getPost('add');
			if ($formData) {
				
				$model = $this->_getModel('HmStore');
				$add   = $this->_model;
				
				$row          = $add->createRow();
				$row->name    = $this->_request->getPost('name');
				$row->address = $this->_request->getPost('address');
				$row->quarter = $this->_request->getPost('quarter');
				$row->city    = $this->_request->getPost('city');
				$row->uf      = $this->_request->getPost('uf');
				$row->zip     = $this->_request->getPost('zip');
				$row->phone   = $this->_request->getPost('phone');
				$row->url     = $this->_request->getPost('url');
				$row->email   = $this->_request->getPost('email');
				$row->map     = $this->_request->getPost('map');
				$row->save();
				
				return $this->_helper->redirector('list');
			}
		}
	}
	
	public function editAction()
	{
		$this->initSession();
		$model         = $this->_getModel('HmStore');
		$store_edit    = $this->_model;
		$store_id      = (int)$this->_request->getParam('store_id');
		
		$row = $store_edit->fetchRow('store_id='.$store_id);
		
		if ($this->_request->isPost()) {
			$formData = $this->_request->getPost('edit');
			if ($formData) {
				$store_id     = (int)$this->_request->getPost('store_id');
				$row->name    = $this->_request->getPost('name');
				$row->address = $this->_request->getPost('address');
				$row->quarter = $this->_request->getPost('quarter');
				$row->city    = $this->_request->getPost('city');
				$row->uf      = $this->_request->getPost('uf');
				$row->zip     = $this->_request->getPost('zip');
				$row->phone   = $this->_request->getPost('phone');
				$row->url     = $this->_request->getPost('url');
				$row->email   = $this->_request->getPost('email');
				$row->map     = $this->_request->getPost('map');

				$row->save();
				
				return $this->_helper->redirector('list');
			}
		}
		$this->view->store_edit = $row;	
	}
	
	public function deleteAction()
	{
		$this->initSession();
		$store_id = (int)$this->_request->getParam('store_id');
		if ($store_id > 0) {
			$model = $this->_getModel('HmStore');
			$remove = $this->_model;
			
			$where = 'store_id = '. $store_id;
			$remove->delete($where);
		}
		return $this->_helper->redirector('list');
	}
}